%% This work may be distributed and/or modified under the
%% conditions of the LaTeX Project Public License, either version 1.3
%% of this license or (at your option) any later version.
%% The latest version of this license is in
%%   http://www.latex-project.org/lppl.txt
%% and version 1.3 or later is part of all distributions of LaTeX
%% version 2003/12/01 or later.
%% Modification of original maxima.sty by J.M. Planas made by J. M. Mira mira@um.es
%%

\NeedsTeXFormat{LaTeX2e}
\ProvidesPackage{maxiplot}[2006/07/25]
\RequirePackage{amsmath}
\RequirePackage{graphicx}
\DeclareOption{amsmath}{\maxima@amsmathtrue}

\newcommand{\macfile}{\jobname.mac}
\newcommand{\mxxfile}{\jobname.mxx}

\newcounter{maxima@count}
\def\maxima@out{-1}
\newcommand{\maximacount}{\value{maxima@count}}

\newif\ifmaximaloaded
\newif\ifmaxima@autoinsertblock
\newif\ifmaxima@texout
\newif\ifmaxima@amsmath
\maxima@amsmathfalse

\ProcessOptions\relax


\newcommand{\maximacurrent}{\ifmaximaloaded\csname \maxima@current\endcsname\fi}

\AtBeginDocument{%
  \InputIfFileExists{\mxxfile}%
  {\maximaloadedtrue}%
  {\maximaloadedfalse}%
}

\AtEndDocument{%
    \immediate\closeout\maxima@out
    \write18{@echo Processing Maxima...}
    \write18{maxima -b \macfile >\macfile.log}
    \write18{@echo Maxima processed. See \macfile.log for details}
}

\newcommand{\IfMaximaLoaded}[1]{\ifmaximaloaded#1\fi}

\def\maxima@init{%
  \ifnum\maxima@out=-1
    \newwrite\maxima@out
    \immediate\openout \maxima@out \macfile
    \immediate\write\maxima@out{load("max-tex-init.mac")$}
    \ifmaxima@amsmath
      \immediate\write\maxima@out{:lisp(defprop $matrix amsmath-matrix tex)}%
      \fi
    \immediate\write\maxima@out{write_stdout("\mxxfile")$}
    \fi%
}

\def\@@imaxima#1{%
  \immediate\write\maxima@out{#1}
  \ifmaxima@texout
    \immediate\write\maxima@out{,print(rbrace))$}
    \fi%
  \endgroup
  \ifmaxima@autoinsertblock \maximacurrent \fi
}

\long\def\@@maxima#1\end{%
  \immediate\write\maxima@out{#1}
  \ifmaxima@texout
    \immediate\write\maxima@out{,print(rbrace))$}
    \fi%
  \endgroup
  \ifmaxima@autoinsertblock \maximacurrent \fi
  \end
}

\def\@maxima@common{%
\begingroup
  \obeylines
  \obeyspaces
  \def\\{\noexpand\\}
  \newlinechar=`\^^M  % Piet van Oostrum: newline fix on MacOSX
  \catcode`\"11\relax
  \catcode`\&11\relax
  \catcode`\%11\relax
  \maxima@init
  \ifmaxima@texout
    \stepcounter{maxima@count}
    \immediate\write\maxima@out{append_stdout("\mxxfile", print("\\newcommand{\\\maxima@current}",lbracep),}%
    \fi
}

\newenvironment{maxima}[1][maxima\romannumeral\maximacount]{%
  \global\def\maxima@current{#1}
  \maxima@autoinsertblocktrue
  \maxima@texouttrue
  \@maxima@common\@@maxima}{}
\newenvironment{maxima*}[1][maxima\romannumeral\maximacount]{%
  \global\def\maxima@current{#1}
  \maxima@autoinsertblockfalse
  \maxima@texouttrue
  \@maxima@common\@@maxima}{}

\newenvironment{maximacmd}{\maxima@autoinsertblockfalse\maxima@texoutfalse\@maxima@common\@@maxima}{}
\newcommand{\imaximacmd}{\maxima@autoinsertblockfalse\maxima@texoutfalse\@maxima@common\@@imaxima}%

\newcommand{\imaxima}[1][maxima\romannumeral\maximacount]{%
  \global\def\maxima@current{#1}
  \@ifstar{\maxima@autoinsertblockfalse\maxima@texouttrue\@maxima@common\@@imaxima}%
          {\maxima@autoinsertblocktrue\maxima@texouttrue\@maxima@common\@@imaxima}}

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

\RequirePackage{verbatim}

\newenvironment{@vmaximawrite}%
 {\@bsphack
  \let\do\@makeother\dospecials\catcode`\^^M\active
  \def\verbatim@processline{%
    \immediate\write\maxima@out{\the\verbatim@line}}%
  \verbatim@start}%
 {\@esphack}

\newenvironment{@vmaxima}%
 {\maxima@init
  \ifmaxima@texout
    \stepcounter{maxima@count}
    \immediate\write\maxima@out{append_stdout("\mxxfile", print("\noexpand\\newcommand{\noexpand\\\maxima@current}",lbracep),}%
    \fi
  \@vmaximawrite}
 {\end@vmaximawrite
  \ifmaxima@texout
    \immediate\write\maxima@out{,print(rbrace))$}
    \fi}

\newenvironment{vmaxima}[1][maxima\romannumeral\maximacount]{%
  \global\def\maxima@current{#1}%
  \maxima@texouttrue\@vmaxima}
 {\end@vmaxima\maximacurrent}

\newenvironment{vmaxima*}[1][maxima\romannumeral\maximacount]{%
  \global\def\maxima@current{#1}
  \maxima@texouttrue\@vmaxima}
 {\end@vmaxima}

\newenvironment{vmaximacmd}%
 {\maxima@texoutfalse\@vmaxima}
 {\end@vmaxima}


  
\newwrite\@mainplot
\newif\gnuplot@autoinsertblock

\def\gnuplot{\immediate\openout\@mainplot\jobname.gnuplot
\bgroup\todocaracter\auxplot}
\def\auxplot#1{\immediate\write\@mainplot{#1}\egroup%
\immediate\closeout\@mainplot%
\immediate\write18{gnuplot \jobname.gnuplot}}
\def\makeother#1{\catcode`#1=12\relax}
\def\todocaracter{\makeother\ \makeother\\\makeother\$\makeother\&%
\makeother\#\makeother\_\makeother\%\makeother\~\makeother\|}

